unit UfrmDefaultMdiChildform;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, IniFiles, Main, Menus;

type
  TfrmDefaultMdiChildform = class(TForm)
    procedure FormCreate(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
  private
    { Private declarations }
    menuWindow : TMenuItem;
    procedure init();
    function getWindowsStateAsString: String;
    procedure setWindowsStateAsString( value : String );
  protected
    config : TIniFile;
    procedure loadFormConfig();
    procedure saveFormConfig();  
  public
    { Public declarations }
    procedure menuWindow_click(sender : TObject);Dynamic; 

  end;

var
  frmDefaultMdiChildform: TfrmDefaultMdiChildform;

implementation

{$R *.dfm}

procedure TfrmDefaultMdiChildform.FormCreate(Sender: TObject);
begin
  config := TIniFile.Create('config.ini');
  loadFormConfig();
  menuWindow := TMenuItem.Create(self);
  menuWindow.Caption := Self.Caption;
  menuWindow.OnClick := menuWindow_click;
  FrmMain.addWindowMenu(menuWindow);
end;

procedure TfrmDefaultMdiChildform.FormClose(Sender: TObject;
  var Action: TCloseAction);
begin
  saveFormConfig();
  FreeAndNil(config);
  Action := caFree;
  frmMain.removeWindowMenu(menuWindow);
end;

procedure TfrmDefaultMdiChildform.menuWindow_click(sender: TObject);
begin
  self.Show();
end;

procedure TfrmDefaultMdiChildform.init;
begin
  loadFormConfig();
end;

procedure TfrmDefaultMdiChildform.loadFormConfig;
begin
  setWindowsStateAsString(
     config.ReadString(self.Name,'WindowsState', 'wsNormal'));
  if WindowState <> wsNormal then exit;
  Top   := config.ReadInteger(self.name, 'Top'   ,80);
  Left  := config.ReadInteger(self.name, 'Left'  ,80);
  Width := config.ReadInteger(Self.name, 'Width' ,self.width);
  Height:= config.ReadInteger(self.name, 'Height',self.Height);

end;

procedure TfrmDefaultMdiChildform.saveFormConfig;
begin
  if WindowState <> wsNormal then
  begin
    config.WriteString(self.name, 'WindowsState', getWindowsStateAsString);
    exit;
  end;

  config.WriteInteger(name, 'Top', Top);
  config.WriteInteger(name, 'Left', Left);
  config.WriteInteger(name, 'Width', Width);
  config.WriteInteger(name, 'Height', Height);

end;

function TfrmDefaultMdiChildform.getWindowsStateAsString: String;
begin
  if WindowState = wsNormal then
    result := 'wsNormal'
  else if WindowState = wsMinimized then
    result := 'wsMinimized'
  else result := 'wsMaximized';
end;

procedure TfrmDefaultMdiChildform.setWindowsStateAsString(value: String);
begin
  if upperCase(value) = 'WSMINIZED' then
    WindowState := wsMinimized
  else if UpperCase(value) = 'WSMAXIMIZED' then
    WindowState := wsMaximized
  else WindowState := wsNormal;
end;

end.
